package com.kd.juhaowan.ui.holder;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.flyjingfish.shapeimageviewlib.ShapeImageView;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.model.GiftModel;
import com.kd.juhaowan.ui.listener.OnReciveListener;
import com.kd.juhaowan.utils.GlideHelper;
import com.kd.juhaowan.utils.LogUtil;


import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;


public class FuliDjqBannerPagerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


    LinearLayout fuliListview;
    private Context context;
    private LinearLayout.LayoutParams params;
    private LinearLayout.LayoutParams homeParames;
    private boolean isMine = false;
    private OnReciveListener myListener;

    public FuliDjqBannerPagerViewHolder(@NonNull View itemView, Context context, boolean ism, OnReciveListener listener) {
        super(itemView);
        this.context = context;
        this.isMine = ism;
        this.myListener = listener;
        this.fuliListview = itemView.findViewById(R.id.item_View);
        params =new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.rightMargin= UIUtil.dip2px(context,5);
        homeParames =new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    private int parePosition;
    private List<GiftModel> mModels ;
    public void loadGoodsImage(List<GiftModel> models,int position){
        parePosition = position;
        mModels = models;
        if(models!=null&&models.size()>0){
            fuliListview.removeAllViews();
            LogUtil.debugLog(models.size()+".....why");
            for (GiftModel model:models) {
                View view = LayoutInflater.from(context).inflate(R.layout.item_fuli_djq,null);
                ShapeImageView imageView = view.findViewById(R.id.item_image);
                TextView nameView = view.findViewById(R.id.item_name);
                TextView starView= view.findViewById(R.id.item_star);
                TextView catView = view.findViewById(R.id.item_cat);
                TextView peopleView = view.findViewById(R.id.item_people);
                LinearLayout zhekouLayout = view.findViewById(R.id.item_zhekou_layout);
                TextView zhekouText = view.findViewById(R.id.item_zhekou_text);
                LinearLayout tagListview = view.findViewById(R.id.gamelist_tag);
                TextView lingquView = view.findViewById(R.id.item_lingqu);
                GlideHelper.loadGoodsCover(Glide.with(imageView),imageView,model.getGame_logo());
                nameView.setText(model.getGame_name()+"-"+model.getName());
                starView.setText(model.getGame_score());
                catView.setText(model.getGame_cat_name());
                peopleView.setText(model.getGame_people_are_playing()+"人在玩");

                if(model.getGame_discount()>0){
                    zhekouLayout.setVisibility(View.VISIBLE);
                    zhekouText.setText(model.getGame_discount()+"折直充");
                }else{
                    zhekouLayout.setVisibility(View.GONE);
                }

                String[] tags =model.getGame_tags();
//                LogUtil.debugLog("dafa"+tags==null?"null":tags.length+"");
                if(tags!=null&&tags.length>0){
                    tagListview.setVisibility(View.VISIBLE);
                    tagListview.removeAllViews();
                    for (String adb:tags) {
                        TextView tv=new TextView(context);
                        tv.setGravity(Gravity.CENTER);
                        tv.setBackgroundResource(R.drawable.tag_bg);
                        tv.setPadding(UIUtil.dip2px(context,5),0,UIUtil.dip2px(context,5),0);
                        tv.setText(adb);
                        tv.setTextColor(context.getColor(R.color.login_edit_hint));
                        tv.setTextSize(11);
                        tagListview.addView(tv,params);
                    }


//            searchHistoryAdapter.resetAdapterData(Arrays.asList(tags));
                }else {
                    tagListview.setVisibility(View.GONE);
                }
                if(isMine){
                    if(model.getStatus()==2){
                        lingquView.setBackgroundResource(R.drawable.gift_btn_recived);
                        lingquView.setTextColor(context.getColor(R.color.c60));
                        lingquView.setText("已过期");
                    }else if(model.getStatus()==1){
                        lingquView.setBackgroundResource(R.drawable.gift_btn_recived);
                        lingquView.setTextColor(context.getColor(R.color.c60));
                        lingquView.setText("已使用");
                    }else {
                        lingquView.setBackgroundResource(R.drawable.login_small_btn);
                        lingquView.setTextColor(context.getColor(R.color.white));
                        lingquView.setText("去使用");
                    }
                }else {
                    if(model.getIs_receive()==0){
                        lingquView.setBackgroundResource(R.drawable.login_small_btn);
                        lingquView.setTextColor(context.getColor(R.color.white));
                        lingquView.setText("领取");
                    }else {
                        lingquView.setBackgroundResource(R.drawable.gift_btn_recived);
                        lingquView.setTextColor(context.getColor(R.color.c60));
                        lingquView.setText("已领取");
                    }
                }
                lingquView.setTag(model);
                lingquView.setOnClickListener(this);
                fuliListview.addView(view,homeParames);
            }
        }

    }

    @Override
    public void onClick(View view) {
        if(myListener!=null){

            myListener.onGiftRecive((GiftModel) view.getTag(),isMine,parePosition);
        }
    }
}
